package cn.leedsoft.luma.common.utils;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by leeds on 2016/12/5 13:25.
 */
public class FormatUtil {
    private static final Map<Integer, String> digit2upper = Maps.newTreeMap();
    static {
        digit2upper.put(1, "壹");
        digit2upper.put(2, "贰");
        digit2upper.put(3, "叁");
        digit2upper.put(4, "肆");
        digit2upper.put(5, "伍");
        digit2upper.put(6, "陆");
        digit2upper.put(7, "柒");
        digit2upper.put(8, "捌");
        digit2upper.put(9, "玖");
    }

    /**
     * 人民币中文大写转换
     * @param input
     * @return
     */
    public static String rmb2Uppercase(double input) {
        StringBuilder res = new StringBuilder();

        long inputLong = Double.valueOf(input).longValue();
        res.append(long2Uppercase(inputLong));
        res.append("圆");
        if(inputLong == input) {//整数
            res.append("整");
        } else {//小数
            String numStr = new BigDecimal(input).setScale(2, BigDecimal.ROUND_HALF_DOWN).toPlainString();
            int jiao = Integer.valueOf(String.valueOf(numStr.charAt(numStr.length()-2)));
            int fen = Integer.valueOf(String.valueOf(numStr.charAt(numStr.length()-1)));
            if (jiao == 0) {
                res.append("零");
            } else {
                res.append(digit2upper.get(jiao));
                res.append("角");
            }
            if (fen != 0) {
                res.append(digit2upper.get(fen));
                res.append("分");
            }
        }
        return res.toString();
    }

    /**
     * 整数转为中文大写。
     * @param num
     * @return
     */
    private static String long2Uppercase(long num) {
        if (num > 9999999999999l) {
            throw new RuntimeException("参数错误：数额过大");
        }
        if (num < 0) {
            throw new RuntimeException("参数错误：不可小于零");
        }
        StringBuilder res = new StringBuilder();

        String numStr = String.valueOf(num);

        if (numStr.length() >= 9) {//亿
            long head = Long.valueOf(numStr.substring(0, numStr.length() - 8));
            res.append(long2Uppercase(head));
            res.append("亿");
            long tail = Long.valueOf(numStr.substring(numStr.length() - 8, numStr.length()));
            res.append(long2Uppercase(tail));
        } else if (numStr.length() >= 5) {//万
            res.append(within1e4(Integer.valueOf(numStr.substring(0, numStr.length() - 4))));
            res.append("万");
            res.append(within1e4(Integer.valueOf(numStr.substring(numStr.length()-4, numStr.length()))));
        } else {//万以内
            res.append(within1e4(Long.valueOf(num).intValue()));
        }
        return res.toString();
    }

    /**
     * 万以内整数转为中文大写
     * @param num
     * @return
     */
    private static StringBuilder within1e4(int num) {//万以内
        StringBuilder res = new StringBuilder();

        Map<Integer, String> unitMap = Maps.newTreeMap(((o1, o2) -> o2.intValue() - o1.intValue()));
        unitMap.put(4, "仟");//位数-单位
        unitMap.put(3, "佰");
        unitMap.put(2, "拾");
        unitMap.put(1, "");

        for (int unit : unitMap.keySet()) {
            int val = getValIn(num, unit);

            if (val != 0) {
                res.append(digit2upper.get(val));
                res.append(unitMap.get(unit));
            } else {
                if (res.length() > 0 && !String.valueOf(res.charAt(res.length() -1)).equals("零")
                        && unit != 1) {
                    res.append("零");
                }
            }
        }
        if (StringUtils.endsWith(res.toString(), "零")) {
            res.deleteCharAt(res.length() - 1);
        }
        return res;
    }

    /**
     * 获取一个数字，从右数，第index位上的数字
     * @param num
     * @param index
     * @return
     */
    private static int getValIn(int num, int index) {
        String numStr = String.format("%04d", num);
        if (index <= 0 || index > numStr.length()) {
            throw new RuntimeException("位数越界");
        }
        return Integer.valueOf(String.valueOf(numStr.charAt(numStr.length() - index)));
    }
}
